<template>
  <div>
    <a-form-model
      layout="inline"
      :model="badForm"
    >
      <a-form-model-item>
        <a-switch :loading="loading2" v-model="badForm.bad_click_open" checked-children="开" un-checked-children="关" @change="onChange" />
      </a-form-model-item>
      <a-form-model-item>
        <a-input placeholder="请输入报警次数" :disabled="!badForm.bad_click_open" v-model="badForm.bad_click_count" type="text" />
      </a-form-model-item>
      <a-form-model-item>
        <a-switch :loading="loading3" v-model="badForm.bad_click_open_remind" checked-children="开" un-checked-children="关" @change="onChange1" />
      </a-form-model-item>
      <a-form-model-item>
        <a-input placeholder="输入联系电话" :disabled="!badForm.bad_click_open_remind" v-model="badForm.bad_click_phone" type="text" />
      </a-form-model-item>
      <a-form-model-item>
        <a-button type="primary" :loading="loading1" @click="onSetting">设置</a-button>
      </a-form-model-item>
    </a-form-model>
  </div>
</template>

<script>
  import { getClick, postClick } from '@/api/customer'

  export default {
    name: 'BadSetting',
    data () {
      return {
        loading1: false,
        loading2: false,
        loading3: false,
        badForm: {
          bad_click_open: 1
        }
      }
    },
    created () {
      this.getBadClick()
    },
    methods: {
      onSetting () {
        const app = this
        const data = {}
        app.loading1 = true
        for (const k in app.badForm) {
          data['bad_click_setting[' + k + ']'] = app.badForm[k]
        }
        postClick(data).then(res => {
          if (res.status === 0) {
            app.getBadClick()
          } else {
            app.$warning({
              title: res.msg
            })
          }
        }).finally(() => {
          app.loading1 = false
        })
      },
      getBadClick () {
        const app = this
        getClick().then(res => {
          if (res.status === 0) {
            app.badForm = res.result.bad_click_setting
            app.badForm['bad_click_open'] = parseInt(res.result.bad_click_setting.bad_click_open)
            app.badForm['bad_click_open_remind'] = parseInt(res.result.bad_click_setting.bad_click_open_remind)
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      onChange (value) {
        const app = this
        if (value) {
          app.badForm.bad_click_open = 1
        } else {
          app.badForm.bad_click_open = 0
        }
      },
      onChange1 (value) {
        const app = this
        if (value) {
          app.badForm.bad_click_open_remind = 1
        } else {
          app.badForm.bad_click_open_remind = 0
        }
      }
    }
  }
</script>

<style scoped>

</style>
